#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"
char* dirpath,*foldername;

char* createdir (char* path, char* folder)
{
	char result[100];
	if(root==NULL)
	{
		strcpy(result,"makedir_FAILURE ");
		strcat(result,ERR_VFS_MAKEDIR_05);
		sprintf(str, "%s",result);
		return str;
		
	}
	if(path==NULL || strlen(folder)==0)
	{
		strcpy(result,"makedir_FAILURE ");
		strcat(result,ERR_VFS_MAKEDIR_00);
		sprintf(str, "%s",result);
		return str;
	}
	
	int ij=0, flag=0;
	while(folder[ij]!='\0')
	{
		if(folder[ij]=='/')
			flag = 1;
			ij++;
	}
	if(flag == 1)
	{
		strcpy(result,"makedir_FAILURE ");
		strcat(result,ERR_VFS_MAKEDIR_02);
		sprintf(str, "%s",result);
		return str;
	}
	int l;
	//printf("path1:%s\n",path);
	//printf("%s\n",root->data);
	char path1[100];
	//path1=path;
	strcpy(path1,path);
	//printf("path1:%s\n",path1);
	if(ifexists(path1,root,folder) == 1)
	{
		strcpy(result,"makedir_FAILURE ");
		strcat(result,ERR_VFS_MAKEDIR_03);
		sprintf(str, "%s",result);
		return str;
	}


	//displayTree(root);

	int i;

	char bstpath[100];
	strcpy(bstpath,path); 
	strcat(path,folder);
	//printf("b4 %s\n",path);
	dirpath=(char*)malloc(100);
	sprintf(dirpath,"%s",path);
	foldername=malloc(100);
	sprintf(foldername,"%s",folder);
	//printf("before insert into tree %s,%s",path,root->data);
	i = insertIntoTree(path,root,1);
	//path = "";
	//displayTree(root);
	
		return "makedir_SUCCESS";
	
	
}

int getDirIndex(char parentpath[100], char fname[40])
{	
//printf("@%s@ ",dirpath);
	int i;
	int sz=main_header_var->max_no_file_descriptors;
	if(parentpath[strlen(parentpath)-1]!='/')
		strcat(parentpath,"/");
	for(i=0;i<sz;i++)
	{
	
		if(fl[i].a == 0)
		{
				//printf("inside:%d",i);
		//	l=i;
			fl[i].a = 1;
			strcpy(fd[i].location_path,parentpath); //printf("#%s# ",parentpath);
			strcpy(fd[i].file_name,fname);
			strcpy(fd[i].file_type,"folder");
			fd[i].block_number = 0 ;
			fd[i].file_size = 0; 
			main_header_var->no_used_file_descriptors=main_header_var->no_used_file_descriptors+1;
			

			bstroot=(bstnode *)bst_insert(bstroot,fd[i].location_path,i);
			
			int hash_index=0;
			hash_index=hash(fd[i].file_name);
			insert_into_list(hash_index,fd[i].file_name,fd[i].location_path);
			//display();
			//displayTree(root);
			//printf("%d\n");
			break;
		}
		
	}
	return i;
}



